package com.hgyc.mom.eam.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamEquipmentMaintainProgram;
import com.hgyc.mom.eam.vo.EamEquipmentMaintainProgramParam;
import com.hgyc.mom.eam.service.EamEquipmentMaintainProgramService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 点检保养项目Controller
 *
 * @author fwj
 * @since 2025-08-10
 */
@Tag(name = "点检保养项目控制器", description = "点检保养项目控制器")
@RestController
@RequestMapping("/eam/maintain/program")
public class EamEquipmentMaintainProgramController {
    @Autowired
    private EamEquipmentMaintainProgramService eamEquipmentMaintainProgramService;

    @Operation(summary = "查询点检保养项目列表", description = "查询点检保养项目列表")
    @PostMapping("/pageList")
    public Result<IPage<EamEquipmentMaintainProgram>> pageList(@Parameter(description = "查询参数") @RequestBody EamEquipmentMaintainProgramParam eamEquipmentMaintainProgramParam) {
        Page<EamEquipmentMaintainProgram> page = new Page<>(eamEquipmentMaintainProgramParam.getPage(), eamEquipmentMaintainProgramParam.getPageSize());
        EamEquipmentMaintainProgram eamEquipmentMaintainProgram = new EamEquipmentMaintainProgram();
        BeanUtils.copyProperties(eamEquipmentMaintainProgramParam, eamEquipmentMaintainProgram);
        QueryWrapper<EamEquipmentMaintainProgram> wrapper = QueryWrapperUtil.build(eamEquipmentMaintainProgram);
        IPage<EamEquipmentMaintainProgram> list = eamEquipmentMaintainProgramService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取点检保养项目详细信息", description = "获取点检保养项目详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "点检保养项目ID") @PathVariable("id") Long id) {
        return Result.success(eamEquipmentMaintainProgramService.getById(id));
    }
    @Operation(summary = "新增点检保养项目", description = "新增点检保养项目")
    @PostMapping
    public Result<?> save(@Parameter(description = "点检保养项目") @RequestBody EamEquipmentMaintainProgram eamEquipmentMaintainProgram) {
        eamEquipmentMaintainProgramService.saveProgram(eamEquipmentMaintainProgram);
        return Result.success();
    }

    @Operation(summary = "修改点检保养项目", description = "修改点检保养项目")
    @PutMapping
    public Result<?> update(@Parameter(description = "点检保养项目") @RequestBody EamEquipmentMaintainProgram eamEquipmentMaintainProgram) {
        eamEquipmentMaintainProgramService.updateProgram(eamEquipmentMaintainProgram);
        return Result.success();
    }

    @Operation(summary = "删除点检保养项目", description = "删除点检保养项目")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(eamEquipmentMaintainProgramService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamEquipmentMaintainProgram> util = new ExcelUtil<>(EamEquipmentMaintainProgram.class);
        util.importTemplateExcel(response, "点检保养项目");
    }

    @Operation(summary = "导出点检保养项目列表", description = "导出点检保养项目列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamEquipmentMaintainProgramParam eamEquipmentMaintainProgramParam) {
        EamEquipmentMaintainProgram eamEquipmentMaintainProgram = new EamEquipmentMaintainProgram();
        BeanUtils.copyProperties(eamEquipmentMaintainProgramParam, eamEquipmentMaintainProgram);
        QueryWrapper<EamEquipmentMaintainProgram> wrapper = QueryWrapperUtil.build(eamEquipmentMaintainProgram);
        List<EamEquipmentMaintainProgram> list = eamEquipmentMaintainProgramService.list(wrapper);
        ExcelUtil<EamEquipmentMaintainProgram> util = new ExcelUtil<>(EamEquipmentMaintainProgram.class);
        util.exportExcel(response, list, "点检保养项目数据");
    }

    @Operation(summary = "导入点检保养项目列表", description = "导入点检保养项目列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamEquipmentMaintainProgram> util = new ExcelUtil<>(EamEquipmentMaintainProgram.class);
        List<EamEquipmentMaintainProgram> eamEquipmentMaintainProgramList = util.importExcel(file.getInputStream());
        String message = eamEquipmentMaintainProgramService.importData(eamEquipmentMaintainProgramList, updateSupport);
        return Result.success(message);
    }
}